//
//  TSBugTapOnCellDidSelectViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "TSBugTapOnCellDidSelectViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CQTSRipeTableView.h>
#import <CQDemoKit/CQTSButtonFactory.h>

#import "UIViewController+TSBug.h"

@interface TSBugTapOnCellDidSelectViewController () <UITableViewDelegate, UIGestureRecognizerDelegate> {
    
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign, readonly) BOOL fixBug;    /**< 是否修复bug（默认否） */

@end

@implementation TSBugTapOnCellDidSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(@"修复didSelect被列表所在视图上的自定义tapGesture拦截", nil);
    
    UIButton *bugStateButton = [CQTSButtonFactory bugButtonWithFixBugHandle:^{
        [self fixBug:NO];
    } reproduceBugHandle:^{
        [self fixBug:YES];
    }];
    [self.view addSubview:bugStateButton];
    [bugStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    CQTSRipeTableView *tableView = [[CQTSRipeTableView alloc] initWithSectionRowCounts:@[@3, @4, @5]];
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(bugStateButton.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(300);
    }];
    self.tableView = tableView;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankViewAction:)];
    tapGesture.delegate = self;
    [self.view addGestureRecognizer:tapGesture];
}



#pragma mark - Event
/*
 *  是否要修复bug
 *
 *  @param shouldFixBug 是否要修复bug（YES:修复bug; NO:重现bug）
 */
- (void)fixBug:(BOOL)shouldFixBug {
    _fixBug = shouldFixBug;
}

/** 点击空白区域的事件 */
- (void)tapBlankViewAction:(UITapGestureRecognizer *)tap {
    [self __showToast:@"响应了TapGR(注意：如果是点cell的要响应cell的点击)" centerOffset:CGPointMake(0, 100)];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self __showToast:@"响应了Cell的点击" centerOffset:CGPointMake(0, 40)];
}


#pragma mark - UIGestureRecognizerDelegate
/// 决定是否手势继续向下响应（找到cell的时候就不继续了）
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (self.fixBug == NO) {
        return YES; // 不处理此方法的时候，其默认值为YES
    }
    
    UIView *touchView = touch.view;
//    if ([touchView isDescendantOfView:self.tableView]) {
//        CGPoint locationPoint = [touch locationInView:self.tableView];
//        NSIndexPath *clickIndexPath = [self.tableView indexPathForRowAtPoint:locationPoint];
//        //NSIndexPath *clickIndexPath = [self.collectionView indexPathForItemAtPoint:locationPoint];
//        if (clickIndexPath != nil) {
//            return NO;
//        }
//    }
    
    if ([NSStringFromClass([touchView class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
